const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const path = require('path');

const app = express();

const PORT = process.env.PORT || 5173;
const API_TARGET = process.env.API_TARGET || 'http://192.168.110.18:8888';

// 代理 API，避免打包后通过本地静态服务器导致的 405/CORS 问题
app.use('/api', createProxyMiddleware({
  target: API_TARGET,
  changeOrigin: true,
  pathRewrite: { '^/api': '' },
}));

// 静态资源
const distDir = path.join(__dirname, 'dist', 'build', 'h5');
app.use(express.static(distDir));

// 单页应用回退
app.get('*', (req, res) => {
  res.sendFile(path.join(distDir, 'index.html'));
});

app.listen(PORT, () => {
  console.log(`H5 app running at http://localhost:${PORT}`);
  console.log(`Proxying /api to ${API_TARGET}`);
});




